﻿using UnityEditor;
using UnityEngine;
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Hont.C2UTool
{
    public static class ShaderInfoHelper
    {
        public struct ShaderField
        {
            public ShaderUtil.ShaderPropertyType PropType;
            public string Name;
            public object Value;
        }

        public static ShaderField[] GetFields(Material material)
        {
            var shader = material.shader;
            var shaderFieldList = new List<ShaderField>();

            var propCount = ShaderUtil.GetPropertyCount(shader);

            for (int i = 0; i < propCount; i++)
            {
                var shaderField = new ShaderField();

                shaderField.Name = ShaderUtil.GetPropertyName(shader, i);
                shaderField.PropType = ShaderUtil.GetPropertyType(shader, i);

                switch (shaderField.PropType)
                {
                    case ShaderUtil.ShaderPropertyType.Color:
                        shaderField.Value = material.GetColor(shaderField.Name);
                        break;
                    case ShaderUtil.ShaderPropertyType.Float:
                    case ShaderUtil.ShaderPropertyType.Range:
                        shaderField.Value = material.GetFloat(shaderField.Name);
                        break;
                    case ShaderUtil.ShaderPropertyType.TexEnv:
                        break;
                    case ShaderUtil.ShaderPropertyType.Vector:
                        shaderField.Value = material.GetVector(shaderField.Name);
                        break;
                }

                shaderFieldList.Add(shaderField);
            }

            return shaderFieldList.ToArray();
        }
    }
}
